package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.constants.WmNewsMsgConstants;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.IApArticleConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.comment.dtos.CommentConfigDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * APP已发布文章配置表 服务实现类
 * </p>
 *
 * @author lin
 * @since 2025-06-10
 */
@Service
@Slf4j
@Transactional
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements IApArticleConfigService {

    /**
     * 修改文章配置
     *
     * @param map 待修改文章ID, 文章上下架状态
     *            0:下架 1:上架
     */
    @Override
    public void updateByMap(Map<String, Object> map) {
        // 参数校验
        if (map == null || map.isEmpty()) {
            log.error("article参数校验: 参数map不能为空");
            throw new IllegalArgumentException("article参数校验: 参数map不能为空");
        }

        // 获取并校验enable状态
        Integer enable = (Integer) map.get(WmNewsMsgConstants.WM_NEWS_UP_OR_DOWN_STATUS);
        if (enable == null) {
            log.error("article参数校验: enable状态不能为空");
            throw new IllegalArgumentException("article参数校验: enable状态不能为空");
        }
        boolean isDown = enable == 1;

        // 获取并校验articleId
        Object articleIdObj = map.get(WmNewsMsgConstants.WM_NEWS_UP_OR_DOWN_ARTICLE_ID);
        if (articleIdObj == null) {
            log.error("article参数校验: articleId不能为空");
            throw new IllegalArgumentException("article参数校验: articleId不能为空");
        }
        long articleId;
        try {
            articleId = Long.parseLong(articleIdObj.toString());
        } catch (NumberFormatException e) {
            log.error("article参数校验: articleId格式不正确");
            throw new IllegalArgumentException("article参数校验: articleId格式不正确", e);
        }
        int status = isDown ? 0 : 1;
        // 执行更新
        lambdaUpdate()
                .eq(ApArticleConfig::getArticleId, articleId)
                .set(ApArticleConfig::getIsDown, status)
                .update();

        String action = isDown ? "上架" : "下架";

        log.info("article端 文章{}成功, ID: {}, 新状态: {}", action, articleId, status);
    }

    /**
     * 修改文章评论状态
     */
    @Override
    public ResponseResult<AppHttpCodeEnum> updateCommentStatus(CommentConfigDto dto) {
        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,dto.getArticleId())
                .set(ApArticleConfig::getIsComment,dto.getOperation()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
